package com.ysw.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.ysw.entity.Shop;
import com.ysw.service.ShopService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thymeleaf.model.IModel;
import tk.mybatis.spring.annotation.MapperScan;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

@Controller
@MapperScan("com.ysw.dao")
public class ShopController {
    @Autowired
    private ShopService shopService;


    @GetMapping("/shopPage/{pageNum}/{pageSize}")
    public String getShopForPage(@PathVariable(value = "pageNum") Integer pageNum,
                                 @PathVariable(value = "pageSize") Integer pageSize,
                                 Model model) {
        PageInfo<Shop> pageInfo = shopService.getPageInfo(pageNum,pageSize);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }else {
            model.addAttribute("pageInfo", null);
        }
        return "shopList";
    }



    @GetMapping("/showAddShopPage")
    public String showAddShopPage() {
        return "shopAddPage";
    }

    @PostMapping("/addShop")
    public String addShop(Shop shop) {
        shopService.addShop(shop);
        Integer num = shop.getId();
        return "redirect:/shopPage/"+(num/5+1)+"/5";
    }

    //转到货物搜索页面
    @GetMapping("/showFindByShopType")
    public String showFindByShopType() {
        return "showSelectByTypeContaining";
    }
    //模糊搜索功能
    @PostMapping("/shopFindByTypeContaining")
    public String shopFindByNameContaining(@RequestParam(value = "type",required = false) String type,Model model) {
        List<Shop> shopList = shopService.shopSelectByName(type);
        model.addAttribute("shopList", shopList);
        model.addAttribute("msg", "搜索成功！！！");
        return "showSelectByTypeContaining";
    }
    //删除功能
    @RequestMapping("/deleteShop/{id}")
    public String deleteShop(@PathVariable(value = "id") Integer id, RedirectAttributes redirectAttributes) throws Exception {
        int a = shopService.deleteShop(id);
        if (a>0){
            redirectAttributes.addFlashAttribute("smg","删除成功！！！");
        }else {
            redirectAttributes.addFlashAttribute("smg","删除失败！！！");
        }
        shopService.resetAutoIncrement();
        return "redirect:/shopPage/1/5";
    }

    //跳转修改页面
    @GetMapping("/toShopUpdatePage/{id}")
    public String toShopUpdatePage(@PathVariable(value = "id")Integer id,Model model) {
        Shop shop = shopService.shopSelectById(id);
        model.addAttribute("shop", shop);
        return "shopUpdatePage";
    }

    @PostMapping("/shopUpdateModel")
    public String shopUpdateModel(Shop shop,RedirectAttributes redirectAttributes) {
        int a = shopService.updateShop(shop);
        if (a>0){
            redirectAttributes.addFlashAttribute("smg","修改成功！！！");
        }else {
            redirectAttributes.addFlashAttribute("smg","修改失败！！！");
        }
        return "redirect:/shopPage/1/5";
    }

    //导出功能
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        shopService.export(response);
    }


}
